/*
 *
 * Copyright 2011, Ibrahim Arief
 * 
 * This file is part of Bambu Game Backend.
 *
 * Bambu Game Backend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bambu Game Backend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Bambu Game Backend.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package com.appspot.bambugame.server.rest;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.appspot.bambugame.server.PlurkService;
import com.appspot.bambugame.server.data.PlurkPlayerDailyPoint;
import com.google.appengine.api.datastore.QueryResultIterable;
import com.google.appengine.repackaged.com.google.common.collect.Lists;
import com.google.jplurk.Qualifier;
import com.google.jplurk.exception.PlurkException;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class HangmanTopDailyScoreCronServlet extends HttpServlet
{
    /**
     * 
     */
    private static final long serialVersionUID = -8957362320144318364L;

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            // get players
            List<PlurkPlayerDailyPoint> tTopTenPlurkers = getTopNScorer( 10 );
            
            JSONObject tAddResponse = PlurkService.getInstance().plurkAdd( "Top 10 plurkers for today (" + new SimpleDateFormat( "dd-MM-yyyy" ).format( Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ).getTime() ) + ") starting from 07:00 WIB : (updated hourly)", Qualifier.SHARES );
            long tNewPlurkID = tAddResponse.getLong( "plurk_id" );
            
            for (int i = 0; i < tTopTenPlurkers.size(); i++)
            {
                PlurkPlayerDailyPoint tAnswerer = tTopTenPlurkers.get( i );
                
                System.out.println("User with id " + tAnswerer.PLURK_ID + " got " + tAnswerer.POINT);
                
                plurkTopScoreAnswerer(tNewPlurkID, i+1, tAnswerer);
            }
            
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            System.out.println("Exception " + ex.getClass() + ", " + ex.getMessage() + " : " + Arrays.deepToString( ex.getStackTrace() ));
        }
    }
    
    public void plurkTopScoreAnswerer(long pPlurkID, int pRank, PlurkPlayerDailyPoint pAnswerer) throws PlurkException, JSONException
    {
        String tAnswererNickName = pAnswerer.USER_NAME;
        PlurkService.getInstance().responseAdd( String.valueOf(pPlurkID), pRank + ". @" + tAnswererNickName + ", " + pAnswerer.POINT + " points.", Qualifier.SAYS );
    }
    
    public List<PlurkPlayerDailyPoint> getTopNScorer(int pTopN)
    {
        
        
        Objectify tObjectify = ObjectifyService.begin();
        QueryResultIterable<PlurkPlayerDailyPoint> tRetIter = tObjectify.query( PlurkPlayerDailyPoint.class ).filter( "DATE", Long.parseLong( new SimpleDateFormat( "yyyyMMdd" ).format( Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) ).getTime() ) ) ).order( "-POINT" ).limit( pTopN ).fetch();
        
        return Lists.newArrayList( tRetIter );
    }
}
